from spider.item_market_industryoverview import ChannelItem, SubChannelItem
from spider.util import MysqlClient

TBL_NAME = 'market_industryoverview_channel'   # 根据自己的情况，修改表名称
TBL_NAME2 = 'market_industryoverview_subchannel'

class SpiderPipeline:
    client = None

    def open_spider(self, spider):
        self.client = MysqlClient()

    def process_item(self, item, spider):
        if spider.name == 'market_industryoverview':    # 这里一定要判断类型，只有属于自己的Item才能使用
            if isinstance(item, ChannelItem):
                sql = 'INSERT INTO '+TBL_NAME+'(id, name, count) VALUES (%s, %s, %s)'
                self.client.execute(sql, (item['id'], item['name'], item['count']))
        if spider.name == 'market_industryoverview2':
            if isinstance(item, SubChannelItem):
                print('管道', item)
                sql = 'INSERT INTO ' + TBL_NAME2 + '(pid, id, name, covor, subscribed_count, archive_count) VALUES (%s, %s, %s, %s, %s, %s)'
                self.client.execute(sql, (item['pid'], item['id'], item['name'], item.get('covor',''), item['subscribed_count'], item['archive_count']))
        return item # 最后一定要返回item